#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
#define rep(i,l,r) for(int i=(l),i##end=(r);i<=i##end;++i)
#define per(i,r,l) for(int i=(r),i##end=(l);i>=i##end;--i)
#define int long long
#define double long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define rbtree(way) tree<way,null_type,less<way>,rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
const int maxn=1e6+10,maxm=1e6+10,mod=998244353,inf=INT_MAX;
inline int ksm(int x,int k,int mod=mod){
	int ans=1;
	for(;k;k>>=1,x=x*x%mod) if(k&1) ans=ans*x%mod;
	return ans;
}

int T,n,ans,a[maxn],b[maxn],c[maxn];

signed main(){
	freopen("kte.in","r",stdin),freopen("kte.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>T;
	rep(o,1,T){
		int x;
		cin>>x,ans=0;
		rep(i,1,n+1) if(i==n+1) a[++n]=x;
		else if(a[i]==x){
			rep(j,i,--n) swap(a[j],a[j+1]);
			break;
		}
		else if(a[i]>x){
			per(j,n++,i) swap(a[j],a[j+1]);
			a[i]=x,i=iend;
		}
		rep(i,1,n) b[i]=b[i-1]+a[i],c[i]=c[i-1]+a[n-i+1];
		rep(i,1,n) ans+=c[i]-b[i]-max(0ll,c[i-1]-b[i]);
		cout<<ans<<'\n';
	}
	return 0;
}/*
6
2
4
1
5
3
4

*/
